home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / EtchedBorder.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  166 lines

  1. /*
  2.  * @(#)EtchedBorder.java    1.7 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20. package com.sun.java.swing.border;
  21.  
  22. import java.awt.Graphics;
  23. import java.awt.Insets;
  24. import java.awt.Rectangle;
  25. import java.awt.Color;
  26. import java.awt.Component;
  27.  
  28. /**
  29.  * A class which implements a simple etched border which can 
  30.  * either be etched-in or etched-out.  If no highlight/shadow
  31.  * colors are initialized when the border is created, then
  32.  * these colors will be dynamically derived from the background 
  33.  * color of the component argument passed into the paintBorder() 
  34.  * method.
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @version 1.7 02/02/98
  44.  * @author David Kloba
  45.  * @author Amy Fowler
  46.  */
  47. public class EtchedBorder extends AbstractBorder
  48. {
  49.     /** Raised etched type. */
  50.     public static final int RAISED  = 0;
  51.     /** Lowered etched type. */
  52.     public static final int LOWERED = 1;
  53.  
  54.     protected int etchType;
  55.     protected Color highlight;
  56.     protected Color shadow;
  57.  
  58.     /**
  59.      * Creates a lowered etched border whose colors will be derived
  60.      * from the background color of the component passed into 
  61.      * the paintBorder method.
  62.      */
  63.     public EtchedBorder()    {
  64.         this(LOWERED);
  65.     }
  66.  
  67.     /**
  68.      * Creates an etched border with the specified etch-type
  69.      * whose colors will be derived
  70.      * from the background color of the component passed into 
  71.      * the paintBorder method.
  72.      * @param etchType the type of etch to be drawn by the border
  73.      */
  74.     public EtchedBorder(int etchType)    {
  75.         this(etchType, null, null);
  76.     }
  77.  
  78.     /**
  79.      * Creates a lowered etched border with the specified highlight and
  80.      * shadow colors.
  81.      * @param highlight the color to use for the etched highlight
  82.      * @param shadow the color to use for the etched shadow
  83.      */
  84.     public EtchedBorder(Color highlight, Color shadow)    {
  85.         this(LOWERED, highlight, shadow);
  86.     }
  87.  
  88.     /**
  89.      * Creates an etched border with the specified etch-type,
  90.      * highlight and shadow colors.
  91.      * @param etchType the type of etch to be drawn by the border
  92.      * @param highlight the color to use for the etched highlight
  93.      * @param shadow the color to use for the etched shadow
  94.      */
  95.     public EtchedBorder(int etchType, Color highlight, Color shadow)    {
  96.         this.etchType = etchType;
  97.         this.highlight = highlight;
  98.         this.shadow = shadow;
  99.     }
  100.  
  101.     /**
  102.      * Paints the border for the specified component with the 
  103.      * specified position and size.
  104.      * @param c the component for which this border is being painted
  105.      * @param g the paint graphics
  106.      * @param x the x position of the painted border
  107.      * @param y the y position of the painted border
  108.      * @param width the width of the painted border
  109.      * @param height the height of the painted border
  110.      */
  111.     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  112.     int w = width;
  113.     int h = height;
  114.     
  115.     g.translate(x, y);
  116.     
  117.     g.setColor(etchType == LOWERED? getShadowColor(c) : getHighlightColor(c));
  118.     g.drawRect(0, 0, w-2, h-2);
  119.     
  120.     g.setColor(etchType == LOWERED? getHighlightColor(c) : getShadowColor(c));
  121.     g.drawLine(1, h-3, 1, 1);
  122.     g.drawLine(1, 1, w-3, 1);
  123.     
  124.     g.drawLine(0, h-1, w-1, h-1);
  125.     g.drawLine(w-1, h-1, w-1, 0);
  126.     
  127.     g.translate(-x, -y);
  128.     }
  129.  
  130.     /**
  131.      * Returns the insets of the border.
  132.      * @param c the component for which this border insets value applies
  133.      */
  134.     public Insets getBorderInsets(Component c)       {
  135.         return new Insets(2, 2, 2, 2);
  136.     }
  137.  
  138.     /**
  139.      * Returns whether or not the border is opaque.
  140.      */
  141.     public boolean isBorderOpaque() { return true; }
  142.  
  143.     /**
  144.      * Returns which etch-type is set on the etched border.
  145.      */
  146.     public int getEtchType() {
  147.         return etchType;
  148.     }
  149.  
  150.    /**
  151.      * Returns the highlight color of the etched border.
  152.      */
  153.     public Color getHighlightColor(Component c)   {
  154.         return highlight != null? highlight : 
  155.                                        c.getBackground().brighter();
  156.     }
  157.  
  158.    /**
  159.      * Returns the shadow color of the etched border.
  160.      */
  161.     public Color getShadowColor(Component c)   {
  162.         return shadow != null? shadow : c.getBackground().darker();
  163.     }
  164.  
  165. }
  166.